/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package cn.edu.thu.log.preprocessrule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
/**
 * 
 * @author dongzihe
 *
 */
public class OperationRule {

	private ArrayList<TagRule> recordRules;
	private Map<String, OneToMoreUtil> inMap;
	private Map<String, OneToMoreUtil> notInMap;
	private Map<String, String> likeMap;
	private Map<String, String> notLikeMap;
	private Map<String, String> equalMap;
	private Map<String, String> notEqualMap;
	private Map<String, String> equalWithoutCaseMap;
	private Map<String, String> notEqualWithoutCaseMap;

	private Map<String, Integer> smallerMap;
	private Map<String, Integer> biggerMap;
	private Map<String, Integer> smallerEqualMap;
	private Map<String, Integer> biggerEqualMap;
	private Map<String, String> betweenMap;
	private Map<String, String> notBetweenMap;

	private OneToMoreUtil inTemp;
	private OneToMoreUtil notInTemp;
	private String intagname = null;
	private String inrule = null;
	private String notIntagname = null;
	private String notInrule = null;

	private int innum = 0;
	private int notinnum = 0;
	private int likenum = 0;
	private int notlikenum = 0;
	private int equalnum = 0;
	private int notequalnum = 0;
	private int equalwithoutnum = 0;
	private int notequalwithoutnum = 0;
	private int smallernum = 0;
	private int biggernum = 0;
	private int smallerequalnum = 0;
	private int biggerequalnum = 0;
	private int betweennum = 0;
	private int notbetweennum = 0;

	private ArrayList<String> tempList;
/**
 * 
 */
	public OperationRule() {
		recordRules = new ArrayList<TagRule>();
		tempList = new ArrayList<String>();
	}

	/*
	 * 等于，对整数和字符串的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param format parameter
	 */
	public void addEqualRule(String teamname, String tagname, String format) {
		if (equalnum == 0) {
			equalMap = new HashMap<String, String>();
		}
		equalMap.put(tagname, format);
		equalnum++;
		tempList.add(format);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.EQUAL, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 不等于，对整数和字符串的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param format parameter
	 */
	public void addNotEqualRule(String teamname, String tagname, String format) {
		if (notequalnum == 0) {
			notEqualMap = new HashMap<String, String>();
		}
		notEqualMap.put(tagname, format);
		notequalnum++;
		tempList.add(format);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.NOTEQUAL, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 等于（忽略大小写），对字符串的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param format parameter
	 */
	public void addEqualWithoutCaseRule(String teamname, String tagname,
			String format) {
		if (equalwithoutnum == 0) {
			equalWithoutCaseMap = new HashMap<String, String>();
		}
		equalWithoutCaseMap.put(tagname, format);
		equalwithoutnum++;
		tempList.add(format);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.EQUAL_WITHOUTCASE, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 不等于（忽略大小写），对字符串的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param format parameter
	 */
	public void addNotEqualWithoutCaseRule(String teamname, String tagname,
			String format) {
		if (notequalwithoutnum == 0) {
			notEqualWithoutCaseMap = new HashMap<String, String>();
		}
		notEqualWithoutCaseMap.put(tagname, format);
		notequalwithoutnum++;
		tempList.add(format);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.NOTEQUAL_WITHOUTCASE, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 正则表达式定义规则，每个字段只能有一个正则表达式规则
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param format parameter
	 */
	public void addLikeRule(String teamname, String tagname, String format) {
		if (likenum == 0) {
			likeMap = new HashMap<String, String>();
		}

		likeMap.put(tagname, format);
		likenum++;
		tempList.add(format);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.LIKE, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 正则表达式定义规则，每个字段只能有一个正则表达式规则
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param format parameter
	 */
	public void addNotLikeRule(String teamname, String tagname, String format) {
		if (notlikenum == 0) {
			notLikeMap = new HashMap<String, String>();
		}
		notLikeMap.put(tagname, format);
		notlikenum++;
		tempList.add(format);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.NOTLIKE, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 符合子串规则，每个字段可以定义多个符合子串
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param noisestr parameter
	 */
	public void addInRule(String teamname, String tagname, String noisestr) {
		if (innum == 0) {
			inTemp = new OneToMoreUtil();
			inMap = new HashMap<String, OneToMoreUtil>();
		}

		Set<String> str = new HashSet<String>();
		if (!inMap.containsKey(tagname)) {
			inTemp.setTagname(tagname);
			str.add(noisestr);
			inTemp.setStrList(str);
		} else {
			Iterator<Entry<String, OneToMoreUtil>> it = inMap.entrySet()
					.iterator();
			while (it.hasNext()) {
				Entry<String, OneToMoreUtil> entry = it.next();
				str.addAll(entry.getValue().getStrList());
				str.add(noisestr);
				inTemp.setTagname(tagname);
				inTemp.setStrList(str);
				// if(entry.getValue().getStrList().contains(noisestr))

			}
		}
		inMap.put(tagname, inTemp);
		this.intagname = tagname;
		this.inrule = noisestr;
		innum++;
		tempList.addAll(inTemp.getStrList());
		TagRule tagrule = new TagRule(teamname, tagname, RuleNameConstants.IN,
				tempList);
		recordRules.add(tagrule);

	}

	/*
	 * 不符合子串规则，每个字段可以定义多个不符合子串
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param noisestr parameter
	 */
	public void addNotInRule(String teamname, String tagname, String noisestr) {
		if (notinnum == 0) {
			notInTemp = new OneToMoreUtil();
			notInMap = new HashMap<String, OneToMoreUtil>();
		}

		Set<String> str = new HashSet<String>();
		if (!notInMap.containsKey(tagname)) {
			notInTemp.setTagname(tagname);
			str.add(noisestr);
			notInTemp.setStrList(str);
		} else {
			Iterator<Entry<String, OneToMoreUtil>> it = notInMap.entrySet()
					.iterator();
			while (it.hasNext()) {
				Entry<String, OneToMoreUtil> entry = it.next();
				str.addAll(entry.getValue().getStrList());
				str.add(noisestr);
				notInTemp.setTagname(tagname);
				notInTemp.setStrList(str);
				// if(entry.getValue().getStrList().contains(noisestr))

			}
		}
		notInMap.put(tagname, notInTemp);
		this.notIntagname = tagname;
		this.notInrule = noisestr;
		notinnum++;
		tempList.addAll(notInTemp.getStrList());
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.NOTIN, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 小于，对整数的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param value parameter
	 */
	public void addSmallerRule(String teamname, String tagname, Integer value) {
		if (smallernum == 0) {
			smallerMap = new HashMap<String, Integer>();
		}
		smallerMap.put(tagname, value);
		smallernum++;
		tempList.add(value.toString());
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.SMALLER, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 大于，对整数的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param value parameter
	 */
	public void addBiggerRule(String teamname, String tagname, Integer value) {
		if (biggernum == 0) {
			biggerMap = new HashMap<String, Integer>();
		}
		biggerMap.put(tagname, value);
		biggernum++;
		tempList.add(value.toString());
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.BIGGER, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 不大于，对整数的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param value parameter
	 */
	public void addSmallerEqualRule(String teamname, String tagname,
			Integer value) {
		if (smallerequalnum == 0) {
			smallerEqualMap = new HashMap<String, Integer>();
		}
		smallerEqualMap.put(tagname, value);
		smallerequalnum++;
		tempList.add(value.toString());
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.SMALLEREQUAL, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 不小于，对整数的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param value parameter
	 */
	public void addBiggerEqualRule(String teamname, String tagname,
			Integer value) {
		if (biggerequalnum == 0) {
			biggerEqualMap = new HashMap<String, Integer>();
		}
		biggerEqualMap.put(tagname, value);
		biggerequalnum++;
		tempList.add(value.toString());
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.BIGGEREQUAL, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 在某个闭区间，对整数的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param valueinterval parameter
	 */
	public void addBetweenRule(String teamname, String tagname,
			String valueinterval) {
		if (betweennum == 0) {
			betweenMap = new HashMap<String, String>();
		}
		betweenMap.put(tagname, valueinterval);
		betweennum++;
		tempList.add(valueinterval);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.BETWEEN, tempList);
		recordRules.add(tagrule);
	}

	/*
	 * 不在某个闭区间，对整数的操作
	 */
	/**
	 * 
	 * @param teamname parameter
	 * @param tagname parameter
	 * @param valueinterval parameter
	 */
	public void addNotBetweenRule(String teamname, String tagname,
			String valueinterval) {
		if (notbetweennum == 0) {
			notBetweenMap = new HashMap<String, String>();
		}
		notBetweenMap.put(tagname, valueinterval);
		notbetweennum++;
		tempList.add(valueinterval);
		TagRule tagrule = new TagRule(teamname, tagname,
				RuleNameConstants.NOTBETWEEN, tempList);
		recordRules.add(tagrule);
	}

	public Map<String, OneToMoreUtil> getAllInRules() {
		return inMap;
	}

	public Map<String, OneToMoreUtil> getAllNotInRules() {
		return notInMap;
	}

	public Map<String, String> getAllLikeRules() {
		return likeMap;
	}

	public Map<String, String> getAllNotLikeRules() {
		return notLikeMap;
	}

	public Map<String, String> getAllEqualRules() {
		return equalMap;
	}

	public Map<String, String> getAllNotEqualRules() {
		return notEqualMap;
	}

	public Map<String, String> getAllEqualWithoutCaseRules() {
		return equalWithoutCaseMap;
	}

	public Map<String, String> getAllNotEqualWithoutCaseRules() {
		return notEqualWithoutCaseMap;
	}

	public Map<String, Integer> getAllSmallerRules() {
		return smallerMap;
	}

	public Map<String, Integer> getAllBiggerRules() {
		return biggerMap;
	}

	public Map<String, Integer> getAllSmallerEqualRules() {
		return smallerEqualMap;
	}

	public Map<String, Integer> getAllBiggerEqualRules() {
		return biggerEqualMap;
	}

	public Map<String, String> getAllBetweenRules() {
		return betweenMap;
	}

	public Map<String, String> getAllNotBetweenRules() {
		return notBetweenMap;
	}

}
